package org.deadline.model.user;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.config.EnumerationDataOnDemand;
import org.deadline.model.config.Guid;
import org.deadline.repository.user.UserGroupRepository;
import org.deadline.service.user.UserGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class UserGroupDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<UserGroup> data;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    UserGroupService userGroupService;

	@Autowired
    UserGroupRepository userGroupRepository;

	public UserGroup getNewTransientUserGroup(int index) {
        UserGroup obj = new UserGroup();
        setGuid(obj, index);
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setDescription(obj, index);
        setGroupCode(obj, index);
        setGroupName(obj, index);
        setGroupOwner(obj, index);
        setModificationDate(obj, index);
        return obj;
    }

	public void setGuid(UserGroup obj, int index) {
        Guid embeddedClass = new Guid();
        setGuidTenentId(embeddedClass, index);
        setGuidUuid(embeddedClass, index);
        obj.setGuid(embeddedClass);
    }

	public void setGuidTenentId(Guid obj, int index) {
        String tenentId = "tenentId_" + index;
        if (tenentId.length() > 100) {
            tenentId = tenentId.substring(0, 100);
        }
        obj.setTenentId(tenentId);
    }

	public void setGuidUuid(Guid obj, int index) {
        String uuid = "uuidxxxxxxxxxxxxxxxxxxxxxxxxxx_" + index;
        if (uuid.length() > 32) {
            uuid = uuid.substring(0, 32);
        }
        obj.setUuid(uuid);
    }

	public void setCreationDate(UserGroup obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(UserGroup obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setDescription(UserGroup obj, int index) {
        String description = "description_" + index;
        if (description.length() > 1000) {
            description = description.substring(0, 1000);
        }
        obj.setDescription(description);
    }

	public void setGroupCode(UserGroup obj, int index) {
        String groupCode = "groupCode_" + index;
        if (groupCode.length() > 255) {
            groupCode = groupCode.substring(0, 255);
        }
        obj.setGroupCode(groupCode);
    }

	public void setGroupName(UserGroup obj, int index) {
        String groupName = "groupName_" + index;
        if (groupName.length() > 255) {
            groupName = groupName.substring(0, 255);
        }
        obj.setGroupName(groupName);
    }

	public void setGroupOwner(UserGroup obj, int index) {
        Person groupOwner = personDataOnDemand.getRandomPerson();
        obj.setGroupOwner(groupOwner);
    }

	public void setModificationDate(UserGroup obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public UserGroup getSpecificUserGroup(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        UserGroup obj = data.get(index);
        Long id = obj.getId();
        return userGroupService.findUserGroup(id);
    }

	public UserGroup getRandomUserGroup() {
        init();
        UserGroup obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return userGroupService.findUserGroup(id);
    }

	public boolean modifyUserGroup(UserGroup obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = userGroupService.findUserGroupEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'UserGroup' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<UserGroup>();
        for (int i = 0; i < 10; i++) {
            UserGroup obj = getNewTransientUserGroup(i);
            try {
                userGroupService.saveUserGroup(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            userGroupRepository.flush();
            data.add(obj);
        }
    }
}
